/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Helpers;

import Hibernate.HibernateUtil;
import Entities.Cursussen;
import Entities.Toetsen;
import java.util.List;
import org.hibernate.*;

/**
 *
 * @author Erhan
 */
public class CursusHelper {

    Session session = null;

    /**
     * 
     */
    public CursusHelper() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    //Ophalen alles
    /**
     *
     * @return
     */
    public List<Cursussen> getcursussen() {
        List<Cursussen> cursussenList = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Cursussen");
            cursussenList = (List<Cursussen>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cursussenList;
    }
    

    //Ophalen van 1
    /**
     *
     * @param id
     * @return
     */
    public Cursussen getCursus(int id) {
        Cursussen result = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Cursussen where id=" + id);
            result = (Cursussen) q.list().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


     public Toetsen getToetsbyCursusId(int cid) {
        Toetsen result = null;
        try {
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Toetsen where cursus_id=" + cid);
            result = (Toetsen) q.list().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }


    // Wegschrijven
    /**
     *
     * @param cursus
     */
    public void persistCursus(Cursussen cursus) {
        org.hibernate.Transaction tx = session.beginTransaction();
        tx.begin();
        session.persist(cursus);
        tx.commit();
    }

    // Verwijderen
    /**
     *
     * @param cursus
     */
    public void deleteCursus(Cursussen cursus) {
        org.hibernate.Transaction tx = session.beginTransaction();
        tx.begin();
        session.delete(cursus);
        tx.commit();
      
    }

    // Updaten
    /**
     *
     * @param cursus
     */
    public void updateCursus(Cursussen cursus) {
        org.hibernate.Transaction tx = session.beginTransaction();
        tx.begin();
        session.update(cursus);
        tx.commit();
    }
}